package com.chainyoung.trademarket.mvp.presenter;

import com.chainyoung.common.appcommon.entity.CommonList;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.trademarket.entity.Ad;
import com.chainyoung.trademarket.mvp.contract.InformationContact;

import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

public class InformationListPresenter extends BasePresenter<InformationContact.Model, InformationContact.View> {

    @Inject
    public InformationListPresenter(InformationContact.Model model, InformationContact.View rootView) {
        super(model, rootView);
    }


    public void getInformationList(TreeMap<String, Object> map) {

        addSubscribe(mModel.getInformationList(map)
                .compose(RxUtils.<CommonList<Ad>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<CommonList<Ad>>(mView) {
                    @Override
                    public void onNext(CommonList<Ad> data) {
                        mView.onGetInformationListSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onGetInformationListSuccess(null);
                                return;
                            }
                        }
                        super.onError(e);
                    }
                }));
    }


}

